package com.example.demo.tool;/**
 * @Auter 李孝在
 * @Date 2025/1/16
 */

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demo.exception.AppException;
import com.example.demo.exception.AppExceptionCodeMsg;


import com.example.demo.tool.fileEnums.FileSizeLimitEnum;
import com.example.demo.tool.fileEnums.FileTypeLimitEnum;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;

/**
 * @projectName: demo
 * @package: com.example.demo.tool
 * @className: FileTool
 * @author: Eric
 * @description: TODO
 * @date: 2025/1/16 15:44
 * @version: 1.0
 */
@Component
public class FileUploadTool  {

    @Value("${upload.dir}")
    private String filePath;


    private static FileUploadTool instance;

    @PostConstruct
    public void init() {
        instance = this;
    }
    //全限制上传器
    public static void fileUpload(MultipartFile multipartFile, Long maxSize, FileTypeLimitEnum... allowedFileTypeEnums) throws IOException {
        //大小限制器
        fileSizeLimit(multipartFile,maxSize);
        //类型限制器
        fileTypeLimit(multipartFile,allowedFileTypeEnums);

        String path = instance.filePath+IdBuilder.getIdBySystemDate()+"/";
        File dir = new File(path);
        if (!dir.exists()) {  //判断路径是否存在，不存在则创建
            dir.mkdirs();
        }
        File file = new File(path + multipartFile.getOriginalFilename());

        multipartFile.transferTo(file);
    }
    //全限制上传器:枚举
    public static void fileUpload(MultipartFile multipartFile, FileSizeLimitEnum maxSize, FileTypeLimitEnum... allowedFileTypeEnums) throws IOException {
        fileUpload(multipartFile,maxSize.getMaxSize(),allowedFileTypeEnums);
    }

    //类型限制器
    public static void fileUpload(MultipartFile multipartFile, FileTypeLimitEnum... allowedFileTypeEnums) throws IOException {
        fileUpload(multipartFile, FileSizeLimitEnum.NO_LIMIT,allowedFileTypeEnums);
    }
    //大小限制
    public static void fileUpload(MultipartFile multipartFile,Long maxSize) throws IOException {
        fileUpload(multipartFile, maxSize,FileTypeLimitEnum.NO_LIMIT);
    }
    //大小限制:枚举
    public static void fileUpload(MultipartFile multipartFile,FileSizeLimitEnum fileSizeLimitEnum) throws IOException {
        fileUpload(multipartFile, fileSizeLimitEnum.getMaxSize());
    }


    //无限制上传器
    public static void fileUpload(MultipartFile multipartFile) throws IOException {
        fileUpload(multipartFile,FileSizeLimitEnum.NO_LIMIT,FileTypeLimitEnum.NO_LIMIT);
    }

    //为空无限制
    private static void fileSizeLimit(MultipartFile multipartFile,Long maxSize) {
        if (Objects.nonNull(maxSize)
                &&multipartFile.getSize() > maxSize) {
            throw new AppException(AppExceptionCodeMsg.UPLOAD_TOO_LARGE);
        }
    }
    //为空无限制
    private static void fileTypeLimit(MultipartFile file, FileTypeLimitEnum... allowedFileTypeEnums) {
        //获取后缀
        String fileSuffix = getFileSuffix(file.getOriginalFilename());
        //获取类型集合
        Set<String> allowedFileTypes = FileTypeLimitEnum.getExtensionsForTypes(allowedFileTypeEnums);
        if (CollUtil.isNotEmpty(allowedFileTypes)
                && !allowedFileTypes.contains(fileSuffix.toLowerCase())) {
            throw new AppException(AppExceptionCodeMsg.UPLOAD_TYPE_INVALID);
        }
    }
    //获取文件后缀
    private static String getFileSuffix(String fileName) {
        if (StrUtil.isEmpty(fileName)) return "";
        return FileUtil.extName(fileName);
    }


}
